﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Infrastructures;
using Models;
using System.IO;

namespace SponsorshipWeb.personal
{
    public partial class SponsorInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (id.IsNullOrEmpty())
                    Response.Redirect("../login.html");

                var user = AuthUtil.GetAuthentication();
                if (user == null)
                    Response.Redirect("../login.html");

                //if (user.CompanyID == 0)
                //    Response.Redirect("../guide.html");

                hfProjectID.Value = id;
                //hfCompanyID.Value = user.CompanyID.ToString();
                hfUserID.Value = user.UserID.ToString();
                BindBooks();
                string mode = Request.QueryString["mode"];
                if (mode == "edit")
                {
                    hfMode.Value = mode;
                    BindUI(id);
                }
            }
        }

        private void BindBooks()
        {
            //一：这条社团活动书是审核通过但是还没有达成合同的。
            //二：社团活动书中的社团，必须是还没有达到三条合作合同。

            //所显示的社团的前期必须是显示出来的社团是还没有达到三条合作中的合同。

            //IProjectService projectService = ServiceFactory.CreateInstance<IProjectService>();

            //var lst = projectService.ListProjectByStatusAndContactNO(hfUserID.Value.ToInt(), new List<ProjectStatus> { ProjectStatus.Open });

            //foreach (var item in lst)
            //    ddlBook.Items.Add(new ListItem(item.ProjectName, item.ProjectNO.ToString()));

            //IUtilService utilservice = ServiceFactory.CreateInstance<IUtilService>();
            //var lstAssoc = utilservice.ListRelationshipTo(RelationshipFun.AssociationToStudent, hfUserID.Value.ToInt());

            //foreach (var item in lstAssoc)
            //    ddlAssoc.Items.Add(new ListItem(item.NameFrom, item.RoleFrom.ToString()));
        }

        private void BindUI(string id)
        {
            //IProjectService service = ServiceFactory.CreateInstance<IProjectService>();

            //var project = service.Get<Project>(id);
            //txtName.Text = project.ProjectName;
            //txtStartTime.Text = project.StartTime.ToString(DataFormatter.ShortDate);
            //txtEndTime.Text = project.EndTime.ToString(DataFormatter.ShortDate);
            //ddlBook.SelectedValue = project.Entrants.ToString();
            //txtRemark.Text = project.ProjectRequire;
            //txtUserName.Text = project.UrgentRemark;

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(project.ProjectNO);
            //FileSystem file = lst.FirstOrDefault(x => x.Fun == FileType.SponsorBook);
            //if (file == null)
            //    return;

            //hlBook.NavigateUrl = file.Path;
            //hlBook.Text = file.CtreateTime.ToString(DataFormatter.ShortDate) + "日上传的方案文件： " + file.OldName;
        }

        private void SetValue(ListItemCollection lst, int money)
        {

        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //if (ddlBook.SelectedIndex != 0)
            //{
            //    string projectNo = ddlBook.SelectedItem.Value;
            //    ProjectItem obj = new ProjectItem();
            //    obj.ProjectNO = hfProjectID.Value.ToInt();
            //    obj.IntentionRole = APPRole.Student;
            //    obj.IntentionRoleNO = ddlAssoc.SelectedItem.Value.ToInt();
            //    obj.IntentionContactNO = hfUserID.Value.ToInt();
            //    obj.ProjectNOSelf = projectNo.ToInt();
            //    obj.UpdateTime = DateTime.Now;
            //    obj.CreateTime = DateTime.Now;
            //    obj.ChooseType = ChooseType.StudentChoose;
            //    obj.Status = ProjectItemStatus.Draft;

            //    IProjectService service = ServiceFactory.CreateInstance<IProjectService>();
            //    service.CreateProjectItem(obj);
            //    Response.Redirect("myActivity.html");
            //    return;
            //}

            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuBook.HasFile == false || String.IsNullOrEmpty(fuBook.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //IProjectService projectService = ServiceFactory.CreateInstance<IProjectService>();
            //Project project = null;

            //if (hfMode.Value == "edit")
            //    project = projectService.Get<Project>(hfProjectID.Value);
            //else
            //    project = new Project();

            //project.ProjectName = txtName.Text;
            //project.Money = double.Parse(rblMoney.SelectedItem.Value);
            //project.MoneyName = rblMoney.SelectedItem.Text;
            //project.StartTime = DateTime.Parse(txtStartTime.Text);
            //project.EndTime = DateTime.Parse(txtEndTime.Text);
            //project.ProjectRequire = txtRemark.Text;
            //project.BelongNO = ddlAssoc.SelectedItem.Value.ToInt();
            //project.BelongType = APPRole.Student;
            //project.ContactNO = hfUserID.Value.ToInt();
            //project.UrgentRemark = txtUserName.Text;

            //if (hfMode.Value == "edit")
            //{
            //    projectService.Update(project, hfUserID.Value);
            //}
            //else
            //{
            //    project.Status = ProjectStatus.Approving;
            //    project.BookStatus = CommonStatus.ForView;
            //    projectService.Create(project, hfUserID.Value);

            //    ProjectItem obj = new ProjectItem();
            //    obj.ProjectNO = hfProjectID.Value.ToInt();
            //    obj.IntentionRole = APPRole.Student;
            //    obj.IntentionRoleNO = ddlAssoc.SelectedItem.Value.ToInt();
            //    obj.IntentionContactNO = hfUserID.Value.ToInt();
            //    obj.UpdateTime = DateTime.Now;
            //    obj.CreateTime = DateTime.Now;
            //    obj.ChooseType = ChooseType.StudentChoose;
            //    obj.ProjectNOSelf = project.ProjectNO;
            //    obj.ProjectSelfStatus = CommonStatus.ForView;
            //    obj.Status = ProjectItemStatus.Draft;
            //    projectService.CreateProjectItem(obj);
            //}

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && fuBook.HasFile)
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(project.ProjectNO);
            //}

            //SaveFile(fuBook, FileType.SponsorBook, lst, project.ProjectNO);

            //Response.Redirect("myActivity.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst, int projectNo)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Student\\" + hfUserID.Value + "\\" + projectNo + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.Belong = projectNo;
            //fs.TypeName = APPRole.Project;
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Student/" + hfUserID.Value + "/" + projectNo + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = Int32.Parse(hfUserID.Value);

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}